package com.platform.task.service.impl;

import java.io.IOException;
import java.util.List;

import org.apache.avro.generic.GenericRecord;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.platform.common.util.avro.AvroUtils;
import com.platform.common.util.avro.vo.Avro2Entity;
import com.platform.task.entity.Base_Cyber;

/**
 * 该service为多实例，多线程执行service处理，通过单实例DataHandler将数据保存在集合中。
 * 通过线程定时定量检测集合中的数据，当数据到达指定的值，或者到达指定的时间，执行数据入库
 * @desc 
 */
@Component
@Scope("prototype")
public class MsmqDataServiceHandler {
	private static final Log log = LogFactory.getLog(MsmqDataServiceHandler.class);
	@Autowired
	public DataHandler dataHandler;

	@SuppressWarnings("unchecked")
	public void handlerByAvro(String schemaName, String message) {
		try {
			List<GenericRecord> genericRecords = AvroUtils.deserialization(schemaName, message.getBytes());
			log.debug("===schema: " + schemaName + " | data: " + genericRecords);
			log.debug("data size: " + genericRecords.size());
			// 过滤非空
			if (genericRecords == null || genericRecords.size() == 0) {
				return;
			}
			if (schemaName.equals("Base_Cyber")) {
				List<Base_Cyber> list = new Avro2Entity<Base_Cyber>().avro2javaEntity(genericRecords, Base_Cyber.class);
				log.debug("-------Base_Cyber入库开始--------");
				dataHandler.baseCyberHandler.cache.addAll(list);
				log.debug("-------Base_Cyber入库结束--------");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
